/*操作本地消息*/
yjx.service('messageHandler',['$rootScope','$timeout','ipCookie','$locals',function($rootScope,$timeout,ipCookie,$locals){
    var isMutualConcern = 1;
    var unMutualConcern = 0;
       return {

        /*获取当前用户的localstorage本地消息对象*/
        getMyMessage: function () {
            var messageObj = $locals.get("message")
            if(messageObj){
                var messageObjArr = eval('[' + messageObj + ']');
            }else{
                messageObj = '';
                messageObjArr = [];
            }
            var messageCurArr;
            if(messageObjArr && messageObjArr.length>0){
                for (var k = 0; k < messageObjArr.length; k++) {
                    //console.info(messageObjArr[k]);
                    //console.info(messageObjArr[k].Uid);
                    if (messageObjArr[k].Uid == $rootScope.userId) {
                        messageCurArr = messageObjArr[k].messageList
                        //console.info(messageCurArr);
                        return messageCurArr;
                    }
                }
            }
        },
           /*获取当前用户的unreadUidArr对象*/
           //getMyUnreadUidArr: function () {
           //    var messageObj = $locals.get("message")
           //    if(messageObj){
           //        var messageObjArr = eval('[' + messageObj + ']');
           //    }else{
           //        messageObj = '';
           //        messageObjArr = [];
           //    }
           //    var unreadUidArr;
           //    if(messageObjArr && messageObjArr.length>0){
           //        for (var k = 0; k < messageObjArr.length; k++) {
           //            if (messageObjArr[k].Uid == $rootScope.userId) {
           //                unreadUidArr = messageObjArr[k].unreadUidArr
           //                return unreadUidArr;
           //            }
           //        }
           //    }
           //},

        /*获取消息对象的长度*/
           getCurMessageContentLength: function(messageCurArr, id){
               /*获取与当前窗口的整个本地消息对象*/
               var length = 0;
                   if(messageCurArr && messageCurArr.length>0){
                       for (var i = 0; i < messageCurArr.length; i++) {
                           //console.info(messageCurArr[i]);
                           if (messageCurArr[i].user.userId == id) {
                               length = messageCurArr[i].content.length;
                               return length;
                           }
                       }
                   }
               return length;
           },

        /*获取当前窗口的整个消息对象*/
        getCurMessageAllContent: function (messageCurArr, id) {
            var messageContent = [];
            if(messageCurArr && messageCurArr.length>0){
                for (var i = 0; i < messageCurArr.length; i++) {
                    //console.info(messageCurArr[i]);
                    if (messageCurArr[i].user.userId == id) {
                        //console.info(messageCurArr[i].content);
                        messageContent = messageCurArr[i].content;
                        //this.pushContent(messageCurArr[i].user,messageCurArr[i].content);
                        //console.info(messageContent);
                        return messageContent;
                    }
                }
            }
            return messageContent;
        },

        /*从指定位置开始获取指定数量的消息
        * maxLength：获取指定长度
        * endInx：上一次截取消息的位置*/
           getCurMessageContent: function (messageCurArr, id,maxLength,endInx) {
               console.log(maxLength);
               console.log(endInx)
               var starInd = 0
               if(messageCurArr && messageCurArr.length>0){
                   for (var i = 0; i < messageCurArr.length; i++) {
                       //console.info(messageCurArr[i]);
                       if (messageCurArr[i].user.userId == id) {
                           if(endInx > maxLength ){
                               starInd = endInx - maxLength;
                               console.info(starInd);
                           }
                           var messageContent = messageCurArr[i].content.slice(starInd,endInx); //messageCurArr[i].content被修改
                           console.info(messageContent);
                           return messageContent;
                       }
                   }
               }
           },

         /*将当前窗口的所有消息放在messagelist中*/
           pushContent:function(user,messageContent){
               //var messageCurArr = $rootScope.messageList;
               if( $rootScope.messageList &&  $rootScope.messageList.length>0){
                   for(var k=0 ;k <  $rootScope.messageList.length;k++){
                       //1.存在messagelist 并且存在与当前窗口的本地消息
                       if ( $rootScope.messageList[k].user.userId == user.userId) {
                           //console.info( $rootScope.messageList[k].content);
                           $rootScope.messageList[k].user = user;
                           $rootScope.messageList[k].content = messageContent;
                           //console.info( $rootScope.messageList);
                           break;
                       }else{
                           if(k ==  $rootScope.messageList.length-1){
                               //2.存在messagelist 不存在与当前窗口的本地消息
                 //var messageCurNew = {"userId":user.userId,"content":messageContent,"headPicture":user.headPicture,"isMutualConcern":user.isMutualConcern,"nickName":user.nickName};
                               var messageCurNew = {"user":user,"content":messageContent,"avatar":user.headPicture}
                               $rootScope.messageList.push(messageCurNew)
                           }
                       }
                   }
               }else{
                   //3.不存在messagelist 不存在与当前窗口的本地消息
                   $rootScope.messageList = [];
                   //var messageCurNew = {"userId":user.userId,"content":messageContent,"headPicture":user.headPicture,"isMutualConcern":user.isMutualConcern,"nickName":user.nickName};
                   var messageCurNew = {"user":user,"content":messageContent,"avatar":user.headPicture}
                   $rootScope.messageList.push(messageCurNew)
               }
           },

        /*将消息存储到localstorage
         *user:会话对象
         *messageContent:会话对象的本地消息json数组
         *messageCurArr：当前用户的本地消息对象  */
        pushMessage: function(user,messageContent,messageCurArr){
            //console.info(messageCurArr);
            if(messageCurArr && messageCurArr.length>0){
                for(var k=0 ;k < messageCurArr.length;k++){
                    //1.存在当前用户本地消息 并且存在与当前窗口的本地消息
                    if (messageCurArr[k].user.userId == user.userId) {
                        //console.info(messageCurArr[k].content);
                        messageCurArr[k].user = user;
                        messageCurArr[k].content = messageContent;
                        //console.info(messageCurArr);
                        break;
                    }else{
                        if(k == messageCurArr.length-1){
                            //2.存在当前用户本地消息 不存在与当前窗口的本地消息
                  //var messageCurNew = {"userId":user.userId,"content":messageContent,"headPicture":user.headPicture,"isMutualConcern":user.isMutualConcern,"nickName":user.nickName};
                            var messageCurNew = {"user":user,"content":messageContent,"avatar":user.headPicture}
                            messageCurArr.push(messageCurNew)
                        }
                    }

                }
            }else{
                //3.不存在当前用户本地消息 不存在与当前窗口的本地消息
                messageCurArr = [];
                //var messageCurNew = {"userId":user.userId,"content":messageContent,"headPicture":user.headPicture,"isMutualConcern":user.isMutualConcern,"nickName":user.nickName};
                var messageCurNew = {"user":user,"content":messageContent,"avatar":user.headPicture}
                messageCurArr.push(messageCurNew)
            }

            var messageObj = $locals.get("message")
            if(messageObj){
                var messageObjArr = eval('[' + messageObj + ']');
            }else{
                messageObj = '';
                messageObjArr = [];
            }
            if(messageObj){
                for (var k = 0; k < messageObjArr.length; k++) {
                    //1.存在本地消息 并且存在当前用户的本地消息
                    if (messageObjArr[k].Uid == $rootScope.userId) {
                        messageObjArr[k].messageList = messageCurArr;
                        //console.info(messageCurArr);
                    }
                    else{
                        if(k == messageObjArr.length-1){
                            //2.存在本地消息 不存在当前用户的本地消息
                            messageObjArr.push({"Uid":$rootScope.userId,"messageList":messageCurArr})
                        }
                    }
                }
                var messageObjArrStr = JSON.stringify(messageObjArr);
                $locals.set("message",messageObjArrStr.substr(1,messageObjArrStr.length - 2));//去掉[]
            }else{
                //3.不存在本地消息 不存在当前用户的本地消息
                messageObjArr.push({"Uid":$rootScope.userId,"messageList":messageCurArr})
                var messageObjArrStr = JSON.stringify(messageObjArr);
                $locals.push("message",messageObjArrStr.substr(1,messageObjArrStr.length - 2));//去掉[]
            }

            //console.info($locals.get("message"));
        },

       /*加入localstorage
       *message：当前消息json对象
       * id:发送对象id */
        addToLocal: function(message, user){
            //当前用户的本地消息对象/
            var messageCurArr = this.getMyMessage();
            //当前对话框的消息对象
            var messageContent = [];
            messageContent = this.getCurMessageAllContent(messageCurArr,user.userId);
            if(message) {
                if(messageContent){
                    messageContent.push(message);
                }
                else{
                    messageContent = [];
                    messageContent.push(message);
                }
            }

            //将当前消息加入local
            this.pushMessage(user,messageContent,messageCurArr);
        },

        /*加入messagelist
        */
           addToMessageList: function(message, user){

               //当前对话框中的消息对象
               var messageContent = [];
               messageContent = this.getCurMessageAllContent($rootScope.messageList,user.userId);
               if(messageContent){
                   messageContent.push(message);
               }
               else{
                   messageContent = [];
                   messageContent.push(message);
               }
               //加入messagelist
               this.pushContent(user,messageContent);
           },


        //
        ///*加入unreadUidArr
        //* */
        //pushUnreadUidToLocal   :function(unreadUidArr){
        //    if(unreadUidArr && unreadUidArr.length>0) {
        //        $rootScope.unread=true;
        //    }else{
        //        $rootScope.unread=false;
        //    }
        //    var messageObj = $locals.get("message")
        //    if(messageObj){
        //        var messageObjArr = eval('[' + messageObj + ']');
        //    }else{
        //        messageObj = '';
        //        messageObjArr = [];
        //    }
        //    if(messageObj){
        //        for (var k = 0; k < messageObjArr.length; k++) {
        //            //1.存在本地消息 并且存在当前用户的本地消息
        //            if (messageObjArr[k].Uid == $rootScope.userId) {
        //                messageObjArr[k].unreadUidArr = unreadUidArr;
        //                //console.info(unreadUidArr);
        //            }
        //            else{
        //                if(k == messageObjArr.length-1){
        //                    //2.存在本地消息 不存在当前用户的本地消息
        //                    messageObjArr.push({"Uid":$rootScope.userId,"unreadUidArr":unreadUidArr})
        //                }
        //            }
        //        }
        //        var messageObjArrStr = JSON.stringify(messageObjArr);
        //        $locals.set("message",messageObjArrStr.substr(1,messageObjArrStr.length - 2));//去掉[]
        //    }else{
        //        //3.不存在本地消息 不存在当前用户的本地消息
        //        messageObjArr.push({"Uid":$rootScope.userId,"unreadUidArr":unreadUidArr})
        //        var messageObjArrStr = JSON.stringify(messageObjArr);
        //        $locals.push("message",messageObjArrStr.substr(1,messageObjArrStr.length - 2));//去掉[]
        //    }
        //},
        ///*pushUnreadUidArr*/
        //   pushUnreadUid:function(id){
        //       if($rootScope.unreadUidArr && $rootScope.unreadUidArr.length>0){
        //           var unreadListStr = $rootScope.unreadUidArr.toString();
        //           if(unreadListStr.search(id) <0){
        //               //$timeout(function(){
        //                   $rootScope.unreadUidArr.push(id);
        //               //})
        //
        //           }
        //       }else{
        //           $rootScope.unreadUidArr = [];
        //           $rootScope.unreadUidArr.push(id);
        //       }
        //       this.pushUnreadUidToLocal($rootScope.unreadUidArr);
        //   },
        //
        ///*已读消息删除对应用户*/
        //   deleteUnreadUid:function(unreadUidArr,id){
        //       if(unreadUidArr && unreadUidArr.length>0){
        //           for(var i=0;i<unreadUidArr.length;i++){
        //               if(unreadUidArr[i]==id){
        //                   unreadUidArr.splice(i,1);
        //                   break;
        //               }
        //           }
        //           this.pushUnreadUidToLocal(unreadUidArr);
        //       }
        //
        //   },
         /*删除历史会话对象*/
           deleteByUser: function(id){
               //当前用户的本地消息对象/
               var messageCurArr = this.getMyMessage();
               if(messageCurArr){
                   for (var i = 0; i < messageCurArr.length; i++) {
                       //console.info(messageCurArr[i]);
                       if (messageCurArr[i].user.userId == id) {
                           messageCurArr.splice(i,1);
                           //console.info(messageCurArr);//返回一个新的数组，包含从 start 到 end （不包括该元素）的 arrayObject 中的元素
                           break;
                       }
                   }
               }

               var messageObj = $locals.get("message")
               if(messageObj){
                   var messageObjArr = eval('[' + messageObj + ']');
               }
               if(messageObj){
                   for (var k = 0; k < messageObjArr.length; k++) {
                       //1.存在本地消息 并且存在当前用户的本地消息
                       if (messageObjArr[k].Uid == $rootScope.userId) {
                           messageObjArr[k].messageList = messageCurArr;
                           //console.info(messageCurArr);
                           break;
                       }
                   }
                   var messageObjArrStr = JSON.stringify(messageObjArr);
                   $locals.set("message",messageObjArrStr.substr(1,messageObjArrStr.length - 2));//去掉[]
               }
           },
        /*删除messageList中的消息*/
           deleteListByUser: function(id){
               //当前用户的本地消息对象/
               //var messageCurArr = this.getMyMessage();
               for (var i = 0; i < $rootScope.messageList.length; i++) {
                   //console.info($rootScope.messageList[i]);
                   if ($rootScope.messageList[i].user.userId == id) {
                       $rootScope.messageList.splice(i,1);
                       //console.info($rootScope.messageList);// 方法向/从数组中添加/删除项目，然后返回被删除的项目
                       break;
                   }
               }
                    },

           /*获取非互相关注的临时对象列表
           * 去掉isMutualConcern条件循环出所有的临时会话对象*/
           getTempleUserList: function (messageCurArr) {
               var templeUserList = [];
               if(messageCurArr && messageCurArr.length>0){
                   for (var i = 0; i < messageCurArr.length; i++) {
                       //console.info(messageCurArr[i]);
                       if (messageCurArr[i].user.isMutualConcern == unMutualConcern) {
                           //console.info(messageCurArr[i].content);
                           //var user = {
                           //    userId : messageCurArr[i].userId,
                           //    headPicture:messageCurArr[i].headPicture,
                           //    isMutualConcern:messageCurArr[i].isMutualConcern,
                           //    nickName:messageCurArr[i].nickName,
                           //}
                           var user = messageCurArr[i].user;
                           templeUserList.push(user);
                           //return templeUserList;
                       }
                   }
               }
               return templeUserList;
           },

           /*查询userId是否在templeUserList中存在*/
             isContain : function (userId) {
               var isContain = false;
               if($rootScope.templeUserList.length >0){
                   for(var i = 0;i<$rootScope.templeUserList.length;i++ ){
                       if($rootScope.templeUserList[i].userId == userId){
                           isContain = true;
                           return isContain ;
                       }
                   }
               }
               return isContain;
           },

    /*获取当前用户的未读消息数*/
    getUnreadMsg: function(messageCurArr,userId) {
        var unread = 0;
        if(messageCurArr && messageCurArr.length>0) {
            for(var ii = 0; ii < messageCurArr.length ; ii++) {
                if(messageCurArr[ii].user && messageCurArr[ii].user.userId == userId && messageCurArr[ii].user.unread) {
                    unread = messageCurArr[ii].user.unread;
                }
            }
        }
        return unread;
    },

    /*获取本地未读消息*/
    getTotalUnreadMsg: function(messageCurArr) {
        var totalUnread = 0;
        if(messageCurArr && messageCurArr.length>0) {
            for(var ii = 0; ii < messageCurArr.length ; ii++) {
                if(messageCurArr[ii].user && messageCurArr[ii].user.unread) {
                    totalUnread = totalUnread + messageCurArr[ii].user.unread;
                }
            }
        }
        //console.info(totalUnread);
        return totalUnread;
    }
    }
}])